<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_Grade_Horaria extends MY_Controller {

	function __construct()
	{
		parent::__construct();
                $this->load->helper('html');
                $this->load->helper('url');
	}

	function index(){
            
            $data['serieList'] = $this->em->createQuery('SELECT s FROM models\serie s');
            
            $this->load->view('grade_horaria/v_grade',$data);
	            
	}
        
        function loadTurma(){
            
            extract($_POST);
            
            $turmaList = $this->em->createQuery('SELECT t FROM models\turma t WHERE t.serieCod = ?1');
            $turmaList->setParameter(1,$serieCod);
            
            $data['turmaList']  = $turmaList;
            
            $this->load->view('turma/inc/i_turma',$data);
        }
         
        function loadGrade(){
            
            extract($_POST);
            
            $gradeList = $this->em->createQuery('SELECT g FROM models\grade_horaria g WHERE g.turmaId = ?1');
            $gradeList->setParameter(1,$turmaId);
            
            $data['gradeList']  = $gradeList;

            $this->load->view('grade_horaria/inc/i_grade',$data);
        }       
        function gravarHorario(){
            
            extract($_POST);
            
            $grade = new models\grade_horaria;

            $grade->setDisciplinaCod($disciplinaCod);
            $grade->setProfessorCod($professorCod);
            $grade->setTurmaId($turmaId);
            $grade->setPrimeiroBim($primeiroBim);
            $grade->setSegundoBim($segundoBim);
            $grade->setTerceiroBim($terceiroBim);
            $grade->setQuartoBim($quartoBim);
            
            $this->em->persist($grade);
            $this->em->flush();
            
            echo TRUE;

        }
        
        function novaTurma(){
            
            $data['serieCod'] = $_POST['serie'];
            $this->load->view('turma/p_nova_turma',$data);
            
        }
        
        function novaGrade(){
            $data['serieCod']   = $_POST['serie'];
            $data['turmaId']    = $_POST['turmaId'];
            $this->load->view('grade_horaria/p_nova_grade',$data);
        }
        
        function detalhaDisciplinaGrade(){
            
            extract($_POST);
            
            $grade = $this->em->find('models\Grade_Horaria',$gradeId);
            
            $professor = $this->em->find('models\Funcionario',$grade->getProfessorCod());
            
            $disciplina = $this->em->find('models\Disciplina',$grade->getDisciplinaCod());
            
            $data['grade'] = $grade;
            $data['professorNome'] = $professor->getNomeFunc();
            $data['disciplinaDesc'] = $disciplina->getDisciplinaDesc();
            
            $this->load->view('grade_horaria/p_altera_grade',$data);
            
        }
        
        function alteraGradeHoraria() {
            
            extract($_POST);
            
            $grade = $this->em->find('models\Grade_Horaria',$gradeId);

            $grade->setDisciplinaCod($disciplinaCod);
            $grade->setProfessorCod($professorCod);
            $grade->setPrimeiroBim($primeiroBim);
            $grade->setSegundoBim($segundoBim);
            $grade->setTerceiroBim($terceiroBim);
            $grade->setQuartoBim($quartoBim);
                        
            $this->em->merge($grade);
            $this->em->flush();
            
            echo TRUE;
            
        }
        
        function excluirGradeHoraria(){
            
            extract($_POST);
            
            $grade = $this->em->find('models\Grade_Horaria',$gradeId);
            
            $this->em->remove($grade);
            
            $this->em->flush();
            
            echo TRUE;
        }
        
        function transferirGrade(){
            
            extract($_POST);
            //serieTransf
            $turmaList = $this->em->createQuery('SELECT t FROM models\turma t WHERE t.serieCod = ?1');
            $turmaList->setParameter(1,$serie);
            
             unset($novaTurma);
             foreach ($turmaList->getResult() as $key=>$turma) {
                
                $novaTurma[$key] = new models\Turma;

                $novaTurma[$key]->setAnoLetivo($turma->getAnoLetivo());
                $novaTurma[$key]->setQtdVagas($turma->getQtdVagas());
                $novaTurma[$key]->setRegime($turma->getRegime());
                $novaTurma[$key]->setSerieCod($serieTransf);
                $novaTurma[$key]->setTurmaCod($turma->getTurmaCod());
                $novaTurma[$key]->setTurnoCod($turma->getTurnoCod());
                
                $this->em->persist($novaTurma[$key]);
                $this->em->flush();
                
                $gradeList[$key] =  $this->em->createQuery('SELECT g FROM models\grade_horaria g WHERE g.turmaId = ?1');
                $gradeList[$key]->setParameter(1,$turma->getTurmaId());
                
                unset($novaGrade);
                foreach ($gradeList[$key]->getResult() as $keyGrade => $grade) {
                    
                    $novaGrade[$key][$keyGrade] = new models\Grade_Horaria;
                    
                    $novaGrade[$key][$keyGrade]->setDisciplinaCod($grade->getDisciplinaCod());
                    $novaGrade[$key][$keyGrade]->setProfessorCod($grade->getProfessorCod());
                    $novaGrade[$key][$keyGrade]->setPrimeiroBim($grade->getPrimeiroBim());
                    $novaGrade[$key][$keyGrade]->setSegundoBim($grade->getSegundoBim());
                    $novaGrade[$key][$keyGrade]->setTerceiroBim($grade->getTerceiroBim());
                    $novaGrade[$key][$keyGrade]->setQuartoBim($grade->getQuartoBim());
                    $novaGrade[$key][$keyGrade]->setTurmaId($novaTurma[$key]->getTurmaId());
                    
                   $this->em->persist($novaGrade[$key][$keyGrade]);
                   $this->em->flush();

                }

              

             }
            echo TRUE;
             
             
         
        }

}